import requests
from lxml import etree

url = 'https://tieba.baidu.com/f?ie=utf-8&kw=%E4%BC%A0%E6%99%BA'
headers = {
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
}
r = requests.get(url)
# print(r.text)
html = etree.HTML(r.text)
comment_list = html.xpath('//div[@class="threadlist_abs threadlist_abs_onlyline "]/text()')
print("提取到的个数：", len(comment_list))
g = open('./评论.html', 'w', encoding='utf-8')
i = 1
for temp in comment_list:
    print(temp)
    g.write('第%s个评论' % i)
    g.write(temp)
    i += 1
